package com.shchuangzhi.smartguard.core.command;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import com.shchuangzhi.smartguard.SharedPref;
import com.shchuangzhi.smartguard.core.Util;

import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by j_hu on 11/20/17.
 */

public class ImageCredentialDownloadCommand extends Command {
    private ImageCredentialDownloadHandler handler = new ImageCredentialDownloadHandler(this);

    public ImageCredentialDownloadCommand() {
        super("image.credential.get");
    }

    @Override
    protected OutputStream fill(OutputStream output) {
        return output;
    }

    @Override
    protected CommandHandler getHandler(String commandName) {
        return handler;
    }

    @Override
    protected String getType() {
        return null;
    }

    @Override
    protected String getURL() {
        String url = SharedPref.getInstance().getRemoteServiceURL();
        if (url != null) {
            url = String.format("%s/smartguard/v1/api/image/credential", url.replaceAll("/$",""));;
        }
        return url;
    }

    private class ImageCredentialDownloadHandler implements CommandHandler {

        private ImageCredentialDownloadCommand command;
        private ImageCredentialDownloadHandler(ImageCredentialDownloadCommand command) {
            this.command = command;
        }

        @Override
        public boolean handle(InputStream input) {
            boolean result = false;
            try {
                Gson gson = new Gson();
                String val = Util.readAll(input);
                OSSResult rs = (OSSResult) gson.fromJson(val,
                        new TypeToken<OSSResult>(){}.getType());
                if (rs != null) {
                    this.command.success(rs);
                }

            }catch (Exception e) {
                raiseError(e);
            }
            return result;
        }
    }

    public class OSSResult {
        @SerializedName("RequestId")
        private String requestId;
        @SerializedName("Credentials")
        private OSSCredential credentials;
        @SerializedName("AssumedRoleUser")
        private AssumedRoleUser assumedRoleUser;

        public String getRequestId() {
            return requestId;
        }

        public OSSCredential getCredentials() {
            return credentials;
        }

        public AssumedRoleUser getAssumedRoleUser() {
            return assumedRoleUser;
        }
    }

    public class AssumedRoleUser {
        @SerializedName("AssumedRoleId")
        private String assumedRoleId;
        @SerializedName("Arn")
        private String arn;

        public String getAssumedRoleId() {
            return assumedRoleId;
        }

        public String getArn() {
            return arn;
        }
    }

    public class OSSCredential {
        @SerializedName("AccessKeySecret")
        private String accessKeySecret;
        @SerializedName("AccessKeyId")
        private String accessKeyId;
        @SerializedName("Expiration")
        private String expiration;
        @SerializedName("SecurityToken")
        private String securityToken;

        public String getAccessKeySecret() {
            return accessKeySecret;
        }

        public String getAccessKeyId() {
            return accessKeyId;
        }

        public String getExpiration() {
            return expiration;
        }

        public String getSecurityToken() {
            return securityToken;
        }
    }
}
